﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pinyin4net;
using Pinyin4net.Format;

namespace Creative85.Framework.Utilities
{
    public static class PinyinUtility
    {
        private static HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat() { CaseType = HanyuPinyinCaseType.UPPERCASE, VCharType = HanyuPinyinVCharType.WITH_U_AND_COLON, ToneType = HanyuPinyinToneType.WITH_TONE_NUMBER };
        public static string GetSpelling(char c)
        {
            string s = string.Empty;
            try
            {
                s = PinyinHelper.ToHanyuPinyinStringArray(c, format)[0];
                //最后一位是音调数字
                s = s.Substring(0, s.Length - 1);
            }
            catch { s = c.ToString(); }
            return s;
        }
        public static string GetSpelling(string str)
        {
            StringBuilder sb = new StringBuilder();
            if (str != null)
            {
                string s;
                foreach (char c in str)
                {
                    s = GetSpelling(c);
                    sb.Append(s);
                }
            }
            return sb.ToString();
        }
        public static string GetShortSpelling(char c)
        {
            string s = GetSpelling(c);
            return s.Substring(0, 1);
        }
        public static string GetShortSpelling(string str)
        {
            StringBuilder sb = new StringBuilder();
            if (str != null)
            {
                string s;
                foreach (char c in str)
                {
                    s = GetShortSpelling(c);
                    sb.Append(s);
                }
            }
            return sb.ToString();
        }
    }
}
